<template>
    <div v-show="value"
        class="popup-container"
        :class="[containerClass,
        {fixed: mode == 'fixed', absolute: mode == 'absolute'}]"
        :style="containerStyle"
        @click="onClickContainer">
        <div class="popup" :class="[position, contentClass,]" :style="contentStyle" @click.stop="noop">
            <div class="popup-center">
                <div class="popup-content">
                    <div class="title">{{title}}</div>
                    <div class="message" v-html="message"></div>
                </div>
                <div class="control clearfix">
                    <button v-if="showCancelButton" class="btn btn-mobile btn-rev-primary btn-large btn-cancel" @click="onClickCancel">{{cancelButtonText}}</button>
                    <button class="btn btn-mobile btn-primary btn-large btn-confirm btn-rect" :class="{'btn-block': !showCancelButton, 'single': !showCancelButton}" @click="onClickConfirm">{{confirmButtonText}}</button>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
/**
 *
 */
export default {
    name: 'Popup',
    props: {
        /**
         * 显示与否
         */
        value: {
            required: true,
            type: Boolean,
            default: false,
        },
        /**
         * 固定模式，可以是fixed或者是absolute
         */
        mode: {
            type: String,
            default: 'fixed',
        },
        /**
         * 位置，允许bottom/center/top
         */
        position: {
            type: String,
            default: 'center',
        },
        /**
         * 容器类
         */
        containerClass: {
            type: [ Object, Array ],
        },
        /**
         * 容器样式
         */
        containerStyle: {
            type: [ Object, Array ],
        },
        /**
         * 内容类
         */
        contentClass: {
            type: [ Object, Array ],
        },
        /**
         * 内容样式
         */
        contentStyle: {
            type: [ Object, Array ],
        },
        /*
            * 是否模态
            */
        modal: {
            type: Boolean,
            default: false,
        },
        // 标题
        title: {
            default: '',
        },
        // 文本内容
        message: {
            default: '',
        },
        // 确认文本
        confirmButtonText: {
            default: '确定',
        },
        // 是否显示cancel按钮
        showCancelButton: {
            type: Boolean,
            default: false,
        },
        // cancel按钮文本
        cancelButtonText: {
            default: '取消',
        },
    },
    methods: {
        /**
         * 点击容器，进行关闭
         */
        onClickContainer() {
            if (!this.modal) {
                this.$emit('input', false);
            }
        },
        noop() {
            // do nothing
        },
        onClickCancel() {
            this.$emit('cancel');
        },
        onClickConfirm() {
            this.$emit('confirm');
        },
    },
};
</script>

<style lang="less">
@import "../../lib/style/mixins.less";

.popup-container {
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.7);
    overflow: auto;
    -webkit-overflow-scrolling: touch;

    &.fixed {
        position: fixed;
        z-index: 1;
    }
    &.absolute {
        position: absolute;
    }
    .popup {
        position: absolute;
        left: 0;
        width: 100%;
        margin-left: auto;
        margin-right: auto;
        overflow: auto;
        // text-align: center;
        -webkit-overflow-scrolling: touch;

        // 居中
        // &.center {
        //     top: 50%;
        //     transform: translate3d(0, -50%, 0);
        // }
    }
    // .popup-center {
    //     display: inline-block;
    //     width: 60%;
    //     padding: 30px;
    //     text-align: left;
    //     background-color: @white-color;
    //     border-radius: 4px;
    // }

    .popup-content {
        width: 500rpx;
        padding: 30rpx 50rpx;
    }
    .title {
        padding-bottom: 20px;
        font-size: 15px;
        line-height: 21px;
        color: #333;
        text-align: center;
        font-weight: bold;
        box-sizing: border-box;
    }
    .message {
        font-size: 16px;
        line-height: 22px;
        color: #333;
    }
    .control {
        padding-top: 20px;
    }

    .btn-cancel {
        float: left;
    }
    .btn-confirm {
        float: right;
    }
    .btn-cancel,
    .btn-confirm {
        width: 46%;
        text-align: center;
        &.single {
            width: 100%;
        }
    }
}

#btn-mobile(
    custom,
    rev-primary,
    @primary-color,
    darken(@primary-color, 20%),
    lighten(@primary-color, 20%),
    @white-color,
    @white-color,
    @white-color
);
.btn.btn-mobile {
    #btn-large();
}
</style>
